import { initGlobalState, MicroAppStateActions } from "qiankun";
import { getToken } from "@/utils/token-utils";

const state = {
  token: getToken() || "",
};
// 文档：https://qiankun.umijs.org/zh/api#initglobalstatestate

// 初始化 state
const actions: MicroAppStateActions = initGlobalState(state);

// 监听状态的变化，一旦发送变化，手动更新状态
actions.onGlobalStateChange((newState, prevState) => {
  state.token = newState.token;
});

// eslint-disable-next-line
// @ts-ignore
actions.getGlobalState = () => {
  return state;
};

export default actions;
